package com.quasar.hpatient.module.home_medicine_edit;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.home_medicine.MedicineBean;
import com.quasar.hpatient.dialog.DateDialog;
import com.quasar.hpatient.dialog.DeleteDoubleDialog;
import com.quasar.hpatient.dialog.MedicineDayDialog;
import com.quasar.hpatient.dialog.RadioDialog;
import com.quasar.hpatient.dialog.TimeDialog;
import com.quasar.hpatient.module.home_medicine.MedicineActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.widget.menu.NextView;

/**
 * description: 编辑药单
 * created by kalu on 2018/5/4 10:48
 */
public class MedicineEditActivity extends BaseActivity<MedicineEditPresenter> implements MedicineEditView {

    public static final String MODEL = "MODEL";
    public static final String EDIT_TYPE = "EDIT_TYPE";
    public static final String EDIT_POSITION = "EDIT_POSITION";
    public static final int EDIT_DELETE = 1;
    public static final int EDIT_MODIFY = 2;
    public static final int EDIT_ADD = 3;
    public static final int RESULT_CODE = 1004;


    @Override
    public int initView() {
        return R.layout.activity_home_medicine_edit;
    }

    @Override
    public void initDataLocal() {
        getPresenter().initData();
        final int position = getIntent().getIntExtra(MedicineActivity.POSITION, -1);
        final long patient_id = getIntent().getIntExtra(MedicineActivity.PATIENTID, -1);
        final TextView title = findViewById(R.id.activity_home_medicine_edit_title);
        setText(title, getResources().getString(position != -1 ? R.string.home_medicine_edit_title2 : R.string.home_medicine_edit_title));

        findViewById(R.id.activity_home_medicine_edit_back).setOnClickListener(v -> onBackPressed());

        findViewById(R.id.activity_home_medicine_edit_save).setOnClickListener(v -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_medicine_edit_list);
            final ArrayList<MedicineBean.MedicineItemBean> list = (ArrayList<MedicineBean.MedicineItemBean>) getIntent().getSerializableExtra(MedicineActivity.DATAS);
            getPresenter().saveMedicine(MedicineEditActivity.this, recycler, list, patient_id, position);
        });

        final RecyclerView recycler = findViewById(R.id.activity_home_medicine_edit_list);
        final List<MedicineBean.MedicineItemBean> list = getIntent().getParcelableArrayListExtra(MedicineActivity.DATAS);
        getPresenter().initList(this, recycler, list, position);
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void showTime(int position) {

        TimeDialog dialog = new TimeDialog(this);
        dialog.setOnTimeChangeListener((hour, minute) -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_medicine_edit_list);
            final List<MedicineBean.MedicineItemBean> list = getIntent().getParcelableArrayListExtra(MedicineActivity.DATAS);
            getPresenter().editMedicine(MedicineEditActivity.this, recycler, hour, minute, position);
        });
        dialog.show();
    }

    @Override
    public void showDelete(int position) {
        String title = "确定删除药单吗？";
        DeleteDoubleDialog dialog = new DeleteDoubleDialog(this, title);
        dialog.setOnDialogChangeListener(() -> {
            final ArrayList<MedicineBean.MedicineItemBean> list = getIntent().getParcelableArrayListExtra(MedicineActivity.DATAS);
            getPresenter().deleteMedicine(MedicineEditActivity.this, list, position);
        });
        dialog.show();
        dialog.setHead("确定删除该药品吗？");
        dialog.setInfo("注：调整药单时，请联系你的主治医生");
    }

    @Override
    public void showYear(NextView view) {
        DateDialog dialog = new DateDialog(this);
        dialog.setOnDateChangeListener((year, month, day) -> {
            final String years = getResources().getString(R.string.home_medicine_edit_year, year, month, day);
            view.setText(years);
        });
        dialog.show();
    }

    @Override
    public void showMethod() {

        RadioDialog dialog = new RadioDialog(this);
        dialog.setOnMethodChangeListener(method -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_medicine_edit_list);
            getPresenter().setMethod(recycler, method);
        });
        dialog.show();
        dialog.setList(Arrays.asList("每天服用", "隔天服用", "隔三天服用", "每周服用", "每月服用"));
    }

    @Override
    public void showDay() {

        MedicineDayDialog dialog = new MedicineDayDialog(this);
        dialog.setOnDayChangeListener(day -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_medicine_edit_list);
            getPresenter().setDay(recycler, day);
        });
        dialog.show();
    }

    @Override
    public void goBack(Intent intent) {
        if (null != intent) {
            setResult(RESULT_CODE, intent);
        }
        onBackPressed();
    }
}